<template>
  <div id="estate">
    <h1>{{community}}</h1>
    <h1>欢迎您</h1>
    <div class="n-wrap" v-if="status == 2">
      <div class="btn" @click="goType">开始业主认证</div>
      <p class="tip">{{county}}（宣）</p>
    </div>
    <div class="y-wrap" v-if="status == 1">
      <p>提示：</p>
      <p>您已申请，请等待认证结果的微信通知！</p>
    </div>
  </div>
</template>

<script>
import { Toast, Dialog } from "mand-mobile";
import https from "@/https";
import config from "@/config";
export default {
  components: {
    [Dialog.name]: Dialog
  },
  name: "estate",
  data() {
    return {
      qid: 0,
      uid: 0,
      community: "",
      county: "",
      status: 2 ////判断是否提交过(1-有，2-无)
    };
  },
  methods: {
    getQuarters() {
      let data = localStorage.getItem("saveEstateParams");
      let qid = JSON.parse(data).qid;
      // if (data == "{}") {
      //   qid = this.$route.query.qid;
      // }else {
      //   qid = JSON.parse(data).qid;
      // }
      let uid = this.$store.state.userInfo.id;
      console.log(qid);
      let params = {
        qid,
        uid
      };
      https
        .fetchPost(config.getQuarters, params)
        .then(res => {
          if (res.data.code == 1) {
            this.community = res.data.data.community;
            this.county = res.data.data.county;
            this.status = res.data.data.status;
          } else {
            Toast.failed(res.data.msg, 2000);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    goType() {
      let data = localStorage.getItem("saveEstateParams");
      let qid = JSON.parse(data).qid;
      // let data = JSON.parse(localStorage.getItem("saveEstateParams"));
      // let qid = '';
      // if (!data) {
      //   qid = this.$route.query.qid;
      // }else {
      //   qid = data.qid;
      // }
      let uid = this.$store.state.userInfo.id;
      // localStorage.removeItem("saveEstateParams");
      this.$router.push({
        path: "/smallEstateType",
        query: { qid: qid, uid: uid }
      });
    },
    //获取地址栏的参数
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    }
  },
  created() {
    Toast.loading("加载中...");
    let qid = this.$route.query.qid;
    let estateIndex = {
      qid
    };
    if (JSON.stringify(estateIndex) != "{}") {
      localStorage.setItem("saveEstateParams", JSON.stringify(estateIndex));
    }
    var code = this.getUrlParam("code"); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
    var local = encodeURIComponent("https://xm.sqyzdh.com/smallEstateIndex");
    var APPID = "wx8d06ea96180136be";
    if (code == null || code === "" || code == this.$store.getters.getCode) {
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        APPID +
        "&redirect_uri=" +
        local +
        "&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
    } else {
      this.$store.commit("saveCode", code);
      let params = {
        code
      };
      https
        .fetchGet(config.getLoginStatus, params)
        .then(res => {
          Toast.hide();
          if (res.data.code == 1) {
            let userInfo = res.data.data;
            this.$store.commit("saveUserInfo", userInfo);
            this.getQuarters();
          } else if (res.data.code == 0) {
            let wxInfo = res.data.data;
            this.$store.commit("saveWxInfo", wxInfo);
            this.$router.replace({
              path: "/shareRegister",
              query: {
                type: 4
              }
            });
          } else {
            Toast.failed(res.data.msg);
          }
        })
        .catch(err => {
          Toast.hide();
          console.log(err);
        });
    }
  }
};
</script>

<style  lang="scss" scoped>
$themeColor: #444444;
$themeGold: #d7b987;
#estate {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;
  color: #444444;
  h1 {
    font-size: 70px;
    line-height: 120px;
  }
  .n-wrap {
     display: flex;
    flex-direction: column;
    align-items: center;
  }
  .btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 300px;
    height: 70px;
    font-size: 30px;
    color: #fff;
    margin-top: 250px;
    background: #2f86f6;
    border-radius: 40px;
  }
  .tip {
    text-align: center;
    font-size: 24px;
    color: #999;
    margin-top: 20px;
  }
  .y-wrap {
    font-size: 28px;
    padding: 100px 30px 0;
    color: #f62f2f;
    line-height: 50px;
  }
}
</style>
